//
//  KyShicaiDetailVController.m
//  KCZ_B段项目
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 Sky. All rights reserved.
//

#import "KyShicaiDetailVController.h"
#import "FitHeader.h"
#import "HTTPMethod.h"
#import "changeString.h"
#import <UIImageView+WebCache.h>
#import "KDetailCollectionCell1.h"//食材详情页自定义collectionviewcell第一种;
#import "KShiDetailCaiModel.h"//食材详情菜谱model
#import "KDetailCollectionCell1.h"
#import "KDetailCollectionCell2.h"
#import "KDetailCollectionCell3.h"
#import "KDetailCollectionCell4.h"
#import "KMethodViewController.h"
@interface KyShicaiDetailVController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,pushDelegate>
@property(nonatomic, strong)UICollectionView *collectionview;

@property(nonatomic, strong)UIImageView *headerView;

@property(nonatomic, strong)UIImageView *scrollUnderButtonimage;
@end

@implementation KyShicaiDetailVController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = _navTitle;
    // Do any additional setup after loading the view.
}
-(void)loadView
{
    [super loadView];
    [self getData];
       UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(SCREENWIDTH, SCREENHEIGHT - 64 - 220 - 49);
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _collectionview = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 220, SCREENWIDTH, SCREENHEIGHT - 64 - 220 - 49) collectionViewLayout:flowLayout];
    _collectionview.delegate =self;
    _collectionview.dataSource = self;
    _collectionview.pagingEnabled = YES;
    _collectionview.backgroundColor = [UIColor whiteColor];
    _headerView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 180 * ht)];
    _headerView.backgroundColor = [UIColor greenColor];
    [self creatButton];
    [self.view addSubview:_headerView];

    [self.view addSubview:_collectionview];
    
    [_collectionview registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"identifier"];
    [_collectionview registerClass:[KDetailCollectionCell1 class] forCellWithReuseIdentifier:@"identifier1"];
    [_collectionview registerClass:[KDetailCollectionCell2 class] forCellWithReuseIdentifier:@"identifier2"];
    [_collectionview registerClass:[KDetailCollectionCell3 class] forCellWithReuseIdentifier:@"identifier3"];
    [_collectionview registerClass:[KDetailCollectionCell4 class] forCellWithReuseIdentifier:@"identifier4"];

}
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.translucent = NO;
}
-(void)pushToMethod:(NSString *)ID
{
    KMethodViewController *kvc = [[KMethodViewController alloc]init];
    kvc.methodID = ID;
    [self.navigationController pushViewController:kvc animated:YES];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 0){
    KDetailCollectionCell1 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"identifier1" forIndexPath:indexPath];
        cell.delegate = self;
        cell.ID = _vegetableID;
        return cell;
    }
    if (indexPath.row == 1) {
        KDetailCollectionCell2 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"identifier2" forIndexPath:indexPath];
        cell.ID = _vegetableID;
        return cell;
    }
    if (indexPath.row == 2) {
        KDetailCollectionCell3 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"identifier3" forIndexPath:indexPath];
        cell.ID = _vegetableID;
        return cell;
    }
    if (indexPath.row == 3) {
        KDetailCollectionCell4 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"identifier4" forIndexPath:indexPath];
        cell.ID = _vegetableID;
        return cell;
    }
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];
    return cell;

}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 4;
}
-(void)getData
{
    NSString *postUrlSting = @"http://api.izhangchu.com/";
    NSString *bodyString = [NSString stringWithFormat:@"methodName=MaterialView&material_id=%@&version=4.02",_vegetableID];
    [HTTPMethod getDataByString:postUrlSting BodyDic:[changeString changeStringToDictionary:bodyString] WithDataBlock:^(id data) {
        NSDictionary *dataDic = [data objectForKey:@"data"];
        [_headerView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[dataDic objectForKey:@"image"]]] placeholderImage:[UIImage imageNamed:@""]];
         NSLog(@"%@", [dataDic objectForKey:@"image"]);
    }];

}
-(void)button1Action
{
    [_collectionview setContentOffset:CGPointMake(0, 0)animated:YES];
    [UIView animateWithDuration:0.2 animations:^{
        _scrollUnderButtonimage.frame = CGRectMake(0, 217 * ht, SCREENWIDTH / 4, 3 * ht);
    }];
}
-(void)button2Action
{
    [_collectionview setContentOffset:CGPointMake(SCREENWIDTH, 0)animated:YES];
    [UIView animateWithDuration:0.2 animations:^{
        _scrollUnderButtonimage.frame = CGRectMake(SCREENWIDTH / 4, 217 * ht, SCREENWIDTH / 4, 3 * ht);
    }];

}
-(void)button3Action
{
    [_collectionview setContentOffset:CGPointMake(SCREENWIDTH * 2, 0)animated:YES];
    [UIView animateWithDuration:0.2 animations:^{
        _scrollUnderButtonimage.frame = CGRectMake(SCREENWIDTH / 4 * 2, 217 * ht, SCREENWIDTH / 4, 3 * ht);
    }];

}
-(void)button4Action
{
    [_collectionview setContentOffset:CGPointMake(SCREENWIDTH * 3, 0)animated:YES];
    [UIView animateWithDuration:0.2 animations:^{
        _scrollUnderButtonimage.frame = CGRectMake(SCREENWIDTH / 4 * 3, 217 * ht, SCREENWIDTH / 4, 3 * ht);
    }];

}
-(void)creatButton
{
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
    button1.frame = CGRectMake(0, 180 * ht, SCREENWIDTH / 4, 37 * ht);
    [button1 setTitle:@"相关菜例" forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(button1Action) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];
    button2.frame = CGRectMake(SCREENWIDTH / 4, 180 * ht, SCREENWIDTH / 4, 37 * ht);
    [button2 setTitle:@"选购要诀" forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(button2Action) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button2];
    
    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeSystem];
    button3.frame = CGRectMake(SCREENWIDTH / 4 * 2, 180 * ht, SCREENWIDTH / 4, 37 * ht);
    [button3 setTitle:@"营养功效" forState:UIControlStateNormal];
    [button3 addTarget:self action:@selector(button3Action) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button3];
    
    UIButton *button4 = [UIButton buttonWithType:UIButtonTypeSystem];
    button4.frame = CGRectMake(SCREENWIDTH / 4 * 3, 180 * ht, SCREENWIDTH / 4, 37 * ht);
    [button4 setTitle:@"实用百科" forState:UIControlStateNormal];
    [button4 addTarget:self action:@selector(button4Action) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button4];
    
    _scrollUnderButtonimage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 217 * ht, SCREENWIDTH / 4, 3 * ht)];
    _scrollUnderButtonimage.backgroundColor = [UIColor greenColor];
    [self.view addSubview:_scrollUnderButtonimage];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
